// 获取components目录下每一个组件目录index文件
const files = require.context(
  '@/components',
  true,
  /^\.\/(\w*\/)+index\.(vue|js)$/
)
// console.log(files('./message/index.js'), files.keys())

export default {
  install (Vue, options) {
    files.keys().forEach((path) => {
      const fileType = this.fileType(path, files(path))
      if (fileType === 'component') {
        this.useComponent(Vue, files(path))
      } else if (fileType === 'componentGroup') {
        this.useComponentGroup(Vue, files(path))
      } else {
        this.useServeApi(Vue, files(path))
      }
    })
  },
  fileType (path, file) {
    if (path.lastIndexOf('.js') === -1) {
      return 'component'
    } else {
      if (file.default.install) {
        return 'serveApi'
      } else {
        return 'componentGroup'
      }
    }
  },
  useComponent (Vue, file) {
    const options = file.default
    const name = options.name
    Vue.component('Md' + name, options)
  },
  useServeApi (Vue, file) {
    Vue.use(file.default)
  },
  useComponentGroup (Vue, file) {
    const components = file.default
    for (const key in components) {
      const name = components[key].name
      Vue.component('Md' + name, components[key])
    }
  }
}
